package vip.specimen.system.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.specimen.system.datasource.entity.SystemResourceEntity;
import vip.specimen.system.datasource.entity.SystemResourceKindEntity;
import vip.specimen.system.datasource.entity.SystemResourceSuffixEntity;
import vip.specimen.system.repository.SystemResourceKindRepository;
import vip.specimen.system.datasource.validation.SystemResourceKindValidation;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.DataBaseUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/10 15:01
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
public class SystemResourceKindService {
    @Autowired
    private SystemResourceKindRepository kindRepository;
    @Autowired
    private SystemResourceSuffixService suffixService;
    @Autowired
    private SystemResourceService resourceService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 插入数据
     *
     * @param kindValidation 验证
     * @return 数据
     */
    @Transactional
    public SystemResourceKindEntity insert(SystemResourceKindValidation kindValidation) {
        SystemResourceKindEntity kindEntity = this.kindRepository.save(new SystemResourceKindEntity(kindValidation.getName(), kindValidation.getIntro(), this.suffixService.findAllById(kindValidation.getSuffix()), Boolean.FALSE, kindValidation.getPath()));
        this.suffixService.update(kindEntity.getSuffix(), kindEntity);
        return kindEntity;
    }

    /**
     * 查询全部数据
     *
     * @return 数据集
     */
    public Page<SystemResourceKindEntity> findAll() {
        JSONObject param = CommonUtil.pageParams(this.request);
        return this.kindRepository.findAll(DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime")));
    }

    /**
     * 根据uuid删除
     *
     * @param uuid uuid
     * @return 成功 or 失败
     */
    @Transactional
    public boolean delete(UUID uuid) {
        if (this.kindRepository.existsById(uuid)) {
            this.suffixService.updateKindNull(this.findUuid(uuid).getSuffix());
            this.kindRepository.deleteById(uuid);
            return true;
        }
        return false;
    }

    /**
     * 根据uuid批量删除
     *
     * @param uuids uuid =》 list
     * @return 成功数
     */
    @Transactional
    public int delete(Set<UUID> uuids) {
        int successNum = 0;
        for (UUID uuid : uuids) {
            successNum += this.delete(uuid) ? 1 : 0;
        }
        return successNum;
    }

    /**
     * 更新状态
     *
     * @param uuid 参数
     * @return
     */
    @Transactional
    public SystemResourceKindEntity update(UUID uuid) {
        if (this.kindRepository.existsById(uuid)) {
            SystemResourceKindEntity kindEntity = this.kindRepository.findById(uuid).get();
            kindEntity.setUpdateTime(Calendar.getInstance().getTime());
            kindEntity.setEnable(!kindEntity.isEnable());
            return this.kindRepository.save(kindEntity);
        }
        return null;
    }

    /**
     * 更新数据
     *
     * @param uuid           uuid
     * @param kindValidation 验证
     * @return 数据
     */
    @Transactional
    public SystemResourceKindEntity update(UUID uuid, SystemResourceKindValidation kindValidation) {
        if (this.kindRepository.existsById(uuid)) {
            SystemResourceKindEntity kindEntity = this.kindRepository.findById(uuid).get();
            kindEntity.setUpdateTime(Calendar.getInstance().getTime());
            kindEntity.setIntro(kindValidation.getIntro());
            kindEntity.setName(kindValidation.getName());
            kindEntity.setPath(kindValidation.getPath());
            List<SystemResourceSuffixEntity> suffixList = this.suffixService.findAllById(kindValidation.getSuffix());
            kindEntity.setSuffix(suffixList);
            this.kindRepository.save(kindEntity);
            this.suffixService.update(suffixList, kindEntity);
            return kindEntity;
        }
        return null;
    }

    /**
     * 根据分类名称查找
     *
     * @param name 名称
     * @return 数据集
     */
    public Page<SystemResourceKindEntity> findSystemResourceKindEntityByNameLike(String name) {
        JSONObject param = CommonUtil.pageParams(this.request);
        Pageable pageable = DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime"));
        return this.kindRepository.findSystemResourceKindEntityByNameContainingOrderByCreateTimeDesc(name, pageable);
    }

    /**
     * 根据uuid查询分类
     *
     * @param uuid uuid
     * @return 分类
     */
    public SystemResourceKindEntity findUuid(UUID uuid) {
        return this.kindRepository.findById(uuid).get();
    }

    /**
     * 根据分类uuid 获取资源列表
     *
     * @param uuid uuid
     */
    public Page<SystemResourceEntity> findKindResource(UUID uuid) {
        SystemResourceKindEntity kindEntity = this.findUuid(uuid);
        List<SystemResourceSuffixEntity> suffixEntities = kindEntity.getSuffix();
        return this.resourceService.findResource(suffixEntities);
    }
}
